package com.example.pdr;

import android.content.Context;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private SensorManager sensorManager;
    private TextView txtValue1;
    private TextView txtValue2;
    private TextView txtValue3;
    private TrackingView trackingView;

    private StepDetectionHandler stepDetectionHandler;
    private DeviceAttitudeHandler deviceAttitudeHandler;
    private StepPositioningHandler stepPositioningHandler;

    private Location currentLocation;
    private int stepCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initializeViews();
        initializeSensors();
        initializePositioning();
    }

    @Override
    protected void onResume() {
        super.onResume();
        stepDetectionHandler.startListening();
        deviceAttitudeHandler.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        stepDetectionHandler.stopListening();
        deviceAttitudeHandler.stopListening();
    }

    private void initializeViews() {
        txtValue1 = findViewById(R.id.txt_value1);
        txtValue2 = findViewById(R.id.txt_value2);
        txtValue3 = findViewById(R.id.txt_value3);
        trackingView = findViewById(R.id.tracking_view);
    }

    private void initializeSensors() {
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        stepDetectionHandler = new StepDetectionHandler(sensorManager);
        stepDetectionHandler.setStepDetectionListener(newStep -> {
            stepCounter++;
            Location newLocation = stepPositioningHandler.computeNextLocation(newStep, deviceAttitudeHandler.orientationValues[0]);
            Log.d("LOCATION", newLocation.getxAxis() + " " + newLocation.getyAxis());

            updateUI(newLocation);
            updateTrackingView(newLocation);
        });
        deviceAttitudeHandler = new DeviceAttitudeHandler(sensorManager);
    }

    private void initializePositioning() {
        currentLocation = new Location(0.0f, 0.0f);
        stepPositioningHandler = new StepPositioningHandler(currentLocation);
    }

    private void updateUI(Location newLocation) {
        txtValue1.setText(String.format("最新位置：%.2f, %.2f", newLocation.getxAxis(), newLocation.getyAxis()));
        txtValue2.setText(String.format("您目前的方向角：%.2f", 360 - deviceAttitudeHandler.orientationValues[0] - 90));
        txtValue3.setText(String.format("走过的步数：%d", stepCounter));
    }

    private void updateTrackingView(Location newLocation) {
        int width = trackingView.getWidth();
        int height = trackingView.getHeight();
        trackingView.addPoint(newLocation.getxAxis(), newLocation.getyAxis(), width, height);
    }
}